package com.lanchewuyou.user.driver.lipeiui;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lanchewuyou.user.driver.ui.CarPhotoActivity;
import com.lanchewuyou.user.driver.R;
import com.lanchewuyou.user.driver.ui.UploadPersonPhoActivity;
import com.lanchewuyou.user.driver.modul.Config;
import com.lanchewuyou.user.driver.modul.OkHttpClientManager;
import com.squareup.okhttp.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

/**
 * 已理赔详情展示页
 */
public class CaseInfoActivity extends AppCompatActivity implements View.OnClickListener {
    private Toolbar mToolbar;
    private RelativeLayout caseCallCustom, caseCallOwner, caseTakePhoto, caseTakePersonPhoto;
    private TextView customName, ownerName, ownerPhone, caseType, caseCompany, casePickAdd,
            casePayType, caseMarked, caseCarType, caseYouwei, caseDistance, caseFixInfo,
            caseCustomInfo, caseCustomPho, caseCustomPhoNum, casePhotoType;
    private ImageView caseYouzhen;
    public static String orderId;
    private String customPhone;
    private ProgressDialog progressDialog;
    private static String DRIVERID;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_case_info);
        initViews();
        SharedPreferences sharedPreferences = getSharedPreferences(Config.PASSWORD_PATH, Activity.MODE_PRIVATE);
        DRIVERID = sharedPreferences.getString("driverid", "");
        progressDialog.show();
        Intent intent = getIntent();
        orderId = intent.getStringExtra("caseid");
        OkHttpClientManager.getAsyn(Config.DRIVERORDERDETAIL + "&driverid=" + DRIVERID + "&orderid=" + orderId,
                new OkHttpClientManager.StringCallback() {
                    @Override
                    public void onFailure(Request request, IOException e) {
                        Toast.makeText(CaseInfoActivity.this, "加载失败", Toast.LENGTH_SHORT).show();
                        progressDialog.dismiss();
                    }

                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            String status = jsonObject.getString("status");
                            if (status.equals("1")) {
                                JSONObject info = jsonObject.getJSONObject("info");
                                customPhone = info.getString("ba_mobile");
                                ownerPhone.setText(info.getString("mobile"));
                                customName.setText(info.getString("username"));
                                ownerName.setText(info.getString("chezhu_name"));
                                caseCompany.setText(info.getJSONObject("insuranceiinfo").getString("name"));
                                casePickAdd.setText(info.getString("address"));
                                caseCarType.setText(info.getJSONObject("carinfo").getString("carmodels"));
                                caseYouwei.setText(info.getJSONObject("carinfo").getString("oilinfo") + "/10");
                                caseDistance.setText(info.getJSONObject("carinfo").getString("carmileage"));
                                casePhotoType.setText(info.getJSONObject("carimg").getJSONObject("lists").getJSONObject("7").getInt("count") + "");
                                String accident = info.getString("accident");
                                if (accident.equals("1")) {
                                    caseType.setText("双车事故（第三者）");
                                } else if (accident.equals("2")) {
                                    caseType.setText("单车事故");
                                } else if (accident.equals("3")) {
                                    caseType.setText("双车事故");
                                }
                                String paid = info.getString("paid");
                                if (paid.equals("0")) {
                                    casePayType.setText("线下付款");
                                } else if (paid.equals("1")) {
                                    casePayType.setText("已收费");
                                }
                                caseMarked.setText(info.getString("remark"));
                                progressDialog.dismiss();
                            } else {
                                Toast.makeText(CaseInfoActivity.this, "没有获取到详情", Toast.LENGTH_SHORT).show();
                                progressDialog.dismiss();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                            progressDialog.dismiss();
                        }
                    }
                });

    }

    private void initViews() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        mToolbar.setTitle("订单详情");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        caseCallCustom = (RelativeLayout) findViewById(R.id.case_call_custom);
        caseCallCustom.setOnClickListener(this);
        caseCallOwner = (RelativeLayout) findViewById(R.id.case_call_owner);
        caseCallOwner.setOnClickListener(this);
        caseTakePhoto = (RelativeLayout) findViewById(R.id.case_take_photo);
        caseTakePhoto.setOnClickListener(this);
        caseTakePersonPhoto = (RelativeLayout) findViewById(R.id.case_upload_person_photo);
        caseTakePersonPhoto.setOnClickListener(this);
        customName = (TextView) findViewById(R.id.case_custom_name);
        ownerName = (TextView) findViewById(R.id.case_owner_name);
        ownerPhone = (TextView) findViewById(R.id.case_owner_number);
        caseType = (TextView) findViewById(R.id.case_case_type);
        caseCompany = (TextView) findViewById(R.id.case_company);
        casePickAdd = (TextView) findViewById(R.id.case_pick_address);
        casePayType = (TextView) findViewById(R.id.case_pay_type);
        caseMarked = (TextView) findViewById(R.id.case_marked);
        caseCarType = (TextView) findViewById(R.id.case_car_type);
        caseYouwei = (TextView) findViewById(R.id.case_youwei);
        caseDistance = (TextView) findViewById(R.id.case_distance);
        caseCustomPho = (TextView) findViewById(R.id.case_owner_photo_type);
        caseCustomPhoNum = (TextView) findViewById(R.id.case_owner_photo_number);
        casePhotoType = (TextView) findViewById(R.id.case_photo_type);
        caseYouzhen = (ImageView) findViewById(R.id.case_img_youzhen);
        progressDialog = new ProgressDialog(CaseInfoActivity.this);
        progressDialog.setMessage("请稍等");
        progressDialog.setTitle("加载中");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.case_call_custom:
                Intent i = new Intent(Intent.ACTION_DIAL);
                i.setData(Uri.parse("tel:" + customPhone));
                startActivity(i);
                break;
            case R.id.case_call_owner:
                Intent i1 = new Intent(Intent.ACTION_DIAL);
                i1.setData(Uri.parse("tel:" + ownerPhone.getText().toString()));
                startActivity(i1);
                break;
            case R.id.case_take_photo:
                Intent takephoto = new Intent(CaseInfoActivity.this, CarPhotoActivity.class);
                takephoto.putExtra("from", "caseinfo");
                startActivity(takephoto);
                break;
            case R.id.case_upload_person_photo:
                Intent personphoto = new Intent(CaseInfoActivity.this, UploadPersonPhoActivity.class);
                personphoto.putExtra("from", "caseinfo");
                startActivity(personphoto);
                break;
            default:
                break;

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_case_info, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
